# -*- coding: UTF-8 -*-
from typing import List
from leetcode_helper import TestcaseHelper, SolutionBase


class Solution(SolutionBase):
    # 解题的方法，命名和Leetcode的保持一致即可
    pass

    # TODO: 在SolutionBase里实现了默认的checkResult，如果不适用与特殊题目，需要重写具体的实现
    """
    # 检查结果是否正确的方法，不同的case，检查方法不同
    def check_result(self, expect, result) -> bool:
        pass
    """
    def maxProfit(self, k: int, prices: List[int]) -> int:
        n = len(prices)
        k = min(k, n // 2)
        buy = [[0] * (k + 1) for _ in range(n)]
        sell = [[0] * (k + 1) for _ in range(n)]

        buy[0][0], sell[0][0] = -prices[0], 0
        for i in range(1, k + 1):
            buy[0][i] = sell[0][i] = float("-inf")

        for i in range(1, n):
            buy[i][0] = max(buy[i - 1][0], sell[i - 1][0] - prices[i])
            for j in range(1, k + 1):
                buy[i][j] = max(buy[i - 1][j], sell[i - 1][j] - prices[i])
                sell[i][j] = max(sell[i - 1][j], buy[i - 1][j - 1] + prices[i]);

        return max(sell[n - 1])


if __name__ == '__main__':
    # 按照leetcode的题目说明，将基本的test case填入
    # 每条case的最后一个元素是正确的结果，即期待的结果
    # 使用元组来保存输入的数据，期待的结果
    testcases = [
        (2, [2, 4, 1], 2),
        (2, [3, 2, 6, 5, 0, 3], 7)
    ]

    for case_item in testcases:
        solution = Solution()
        # 获取测试用的case和期待的执行结果
        # TODO: 单独一条的Test case的组成随题目的不同而改变，需要按照题目来修改代码

        input1 = case_item[0]
        input2 = case_item[1]

        expect = case_item[len(case_item) - 1]
        # TODO: 调用对应方法，需要替换具体的方法
        exec_result = solution.maxProfit(input1, input2)

        # 判断执行结果，输出提示信息
        check_result = solution.check_result(expect, exec_result)
        TestcaseHelper.print_case(check_result, case_item, exec_result)

